package com;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @user yuhang
 * @desc
 * @since 2022/2/27
 */
public class XX {


    private static void download(String targetSrc, String imageUrl) throws IOException {
        int byteread = 0;
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            URL url = new URL(imageUrl);
            URLConnection conn = url.openConnection();
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            inStream = conn.getInputStream();
            fs = new FileOutputStream(targetSrc);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("失败，请重试");
        } finally {
            if (fs != null) {
                fs.close();
            }
            if (inStream != null) {
                inStream.close();
            }

        }
    }
    private static void downloadImage(String targetSrc, String imageUrl) throws IOException {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            URL url = new URL(imageUrl);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setReadTimeout(4_000);
            urlConnection.setConnectTimeout(4_000);
            inputStream = urlConnection.getInputStream();
            fileOutputStream = new FileOutputStream(targetSrc);
            int j = 0;
            while ((j = inputStream.read()) != -1) {
                fileOutputStream.write(j);
            }
        } catch (Exception e){
          e.printStackTrace();
            throw new RuntimeException("失败，请重试");
        } finally {
            if (fileOutputStream != null){
                fileOutputStream.close();
            }
            if (inputStream != null){
                inputStream.close();
            }

        }

    }

    public static void main(String[] args) throws Exception {

        String  imageUrl="http://3e3e.i.ximgs.net/0/657620/20220513/20220513150421910_750.jpg";

        for(int k=0;k<10;k++){


            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<20;i++){
                        String name= Thread.currentThread().getId()+"x";
                        String targetSrc="D:/x/"+name+i+"1_750.jpg";
                        try {
                            download(targetSrc,imageUrl);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }).start();

        }

    }

}
